<?php


class Admin extends CI_Controller {

	function __construct()
	{
		parent::__construct();
		$this->load->database();
		$this->load->helper('url');
		$this->load->library('grocery_CRUD');
	}

	function editrights(){
		$this->myuser->is_user_login();
		log_message('debug', "Starting Showpage..page=".$this->uri->segment(1, 0)."\r\n");
		$data=$this->pageview->showpage($this->uri->segment(1, 0));
		$data['menu']=$this->pageview->ShowEditorMenu();
		$crud = new grocery_CRUD();
		$crud->set_theme('datatables');
		$crud->set_table('rights');
		//$crud->set_subject('Pages');
		//$crud->required_fields('page_name');
		$crud->columns('page_id','group_id','deleted');
		$crud->display_as('group_id','Имя группы');
		$crud->display_as('page_id','Имя страницы');
		$crud->display_as('deleted','Удален');
		$crud->set_relation('group_id','groups','group_name');
		$crud->set_relation('page_id','pages','page_name');
		$output = $crud->render();
		$output->menu=$data['menu'];
		//$output->message='ePageId='.$this->input->post('ePageId').'eGroupId='.$this->input->post('eGroupId').'eMyRightsEditorAction='.$this->input->post('eMyRightsEditorAction');
		array_push ( $output->css_files ,'http://'.$this->input->server('HTTP_HOST').'/my.css');
		//array_push ( $output->js_files ,'http://'.$this->input->server('HTTP_HOST').'/my.js');
		$this->load->view('admin_view',$output);
	
	}
	
	function edituig(){
		$this->myuser->is_user_login();
		log_message('debug', "Starting Showpage..page=".$this->uri->segment(1, 0)."\r\n");
		$data=$this->pageview->showpage($this->uri->segment(1, 0));
		$data['menu']=$this->pageview->ShowEditorMenu();
		$crud = new grocery_CRUD();
		$crud->set_theme('datatables');
		$crud->set_table('useringroup');
		//$crud->set_subject('Pages');
		//$crud->required_fields('page_name');
		$crud->columns('user_id','group_id','deleted');
		$crud->display_as('group_id','Имя группы');
		$crud->display_as('user_id','Имя пользователя');
		$crud->display_as('deleted','Удален');
		$crud->set_relation('user_id','users','user_name');
		$crud->set_relation('group_id','groups','group_name');
		$output = $crud->render();
		$output->menu=$data['menu'];
		//$output->message='ePageId='.$this->input->post('ePageId').'eGroupId='.$this->input->post('eGroupId').'eMyRightsEditorAction='.$this->input->post('eMyRightsEditorAction');
		array_push ( $output->css_files ,'http://'.$this->input->server('HTTP_HOST').'/my.css');
		//array_push ( $output->js_files ,'http://'.$this->input->server('HTTP_HOST').'/my.js');
		$this->load->view('admin_view',$output);
	
	}
	
	function encrypt_password_callback($post_array, $primary_key = null)
	{
		$key = 'qawsedrftg';
		$post_array['user_pwd'] = crypt($post_array['user_pwd'],$key);
		return $post_array;
	}
	
	function edituser(){
		$this->myuser->is_user_login();
		log_message('debug', "Starting Showpage..page=".$this->uri->segment(1, 0)."\r\n");
		$data=$this->pageview->showpage($this->uri->segment(1, 0));
		$crud = new grocery_CRUD();
		$crud->set_theme('datatables');
		$crud->set_table('users');
		//$crud->set_subject('Pages');
		//$crud->required_fields('page_name');
		$crud->columns('user_name','user_pwd','block','deleted');
		$crud->display_as('user_name','Имя пользователя');
		$crud->display_as('user_pwd','Пароль пользователя');
		$crud->display_as('block','Блокировка');
		$crud->display_as('deleted','Удален');
		$crud->change_field_type('user_pwd','password');
		$crud->callback_before_insert(array($this,'encrypt_password_callback'));
		$crud->callback_before_update(array($this,'encrypt_password_callback'));
		//$crud->set_relation('parent_id','pages','page_name');
		$output = $crud->render();
		$output->menu=$data['menu'];
		$this->load->view('admin_view',$output);
	
	}
	
	function editpage(){
		//		$this->grocery_crud->set_table('pages');
		//		$output = $this->grocery_crud->render();
		//		$this->load->view('editgroup_view',$output);
	
		$this->myuser->is_user_login();
		log_message('debug', "Starting Showpage..page=".$this->uri->segment(1, 0)."\r\n");
		$data=$this->pageview->showpage($this->uri->segment(1, 0));
		$crud = new grocery_CRUD();
		$crud->set_theme('datatables');
		$crud->set_table('pages');
		$crud->set_subject('Pages');
		$crud->required_fields('page_name');
		$crud->columns('page_name','page_address','parent_id','page_body','dynamic');
		$crud->display_as('parent_id','Parent Name');
		$crud->set_relation('parent_id','pages','page_name');
		$output = $crud->render();
		$output->menu=$data['menu'];
		$this->load->view('admin_view',$output);
	
	}
	
	function editgroup(){
		//		$this->grocery_crud->set_table('pages');
		//		$output = $this->grocery_crud->render();
		//		$this->load->view('editgroup_view',$output);
	
		$this->myuser->is_user_login();
		log_message('debug', "Starting Showpage..page=".$this->uri->segment(1, 0)."\r\n");
		$data=$this->pageview->showpage($this->uri->segment(1, 0));
		$crud = new grocery_CRUD();
		$crud->set_theme('datatables');
		$crud->set_table('groups');
		//$crud->set_subject('Groups');
		//$crud->required_fields('page_name');
		$crud->columns('id','group_name','deleted');
		$crud->display_as('id','Номер группы');
		$crud->display_as('group_name','Имя группы');
		$crud->display_as('deleted','Удален');
		//$crud->set_relation('parent_id','pages','page_name');
		$output = $crud->render();
		$output->menu=$data['menu'];
		$this->load->view('admin_view',$output);
	
	}
	
	//======================================================================================
	// �������� ��������� ��������
	//======================================================================================

	function index()
	{
		$this->myuser->is_user_login();
		log_message('debug', "Starting Showpage..page=".$this->uri->segment(1, 0)."\r\n");
		$data=$this->pageview->showpage($this->uri->segment(1, 0));
		//$data['js_files']=0;
		//$data['css_files']=0;
		$data['output']='';
		$this->load->view('admin_view',$data);
		
	}
	
	//======================================================================================
	// ������������
	//======================================================================================
	function logout(){
		$this->myuser->logout();
	}
}